﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace 城市天然气管道风险管理与决策系统
{
    public partial class Form_menu_1_4 : Form
    {
        public Form_menu_1_4()
        {
            InitializeComponent();
        }

        private void Form_menu_1_4_Load(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
            textBox2.ReadOnly = true;
            textBox3.ReadOnly = true;
            textBox4.ReadOnly = true;

            textBox1.Text = GlobalData.default_pipeline.Id.ToString();
            textBox2.Text = GlobalData.default_pipeline.Name;
            textBox4.Text = GlobalData.default_pipeline.StartLocation;
            textBox3.Text = GlobalData.default_pipeline.EndLocation;

            if (GlobalData.default_pipeline.Accidents != null)
            {
                foreach (var date in GlobalData.default_pipeline.Accidents)
                {
                    listBox1.Items.Add(date.AcDate.ToString());
                }
            }
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime date = dateTimePicker1.Value.Date;
            string level = comboBox1.Text;
            int d_num = 0;
            int i_num = 0;
            double loss = 0;
            string reason = richTextBox1.Text;
            string desc = richTextBox2.Text;
            string process= richTextBox3.Text;
            try
            {
                d_num = int.Parse(textBox5.Text);
                i_num = int.Parse(textBox6.Text);
                loss = double.Parse(textBox7.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入有误");
                return;
            }
            AccidentInfo accident = new AccidentInfo(date, level, d_num, i_num, loss, reason, desc, process);
            if (GlobalData.default_pipeline.Accidents == null) GlobalData.default_pipeline.Accidents = new List<AccidentInfo> { accident };
            else GlobalData.default_pipeline.Accidents.Add(accident);
            listBox1.Items.Add(date.ToString());
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            DateTime date = dateTimePicker1.Value.Date;
            string level = comboBox1.Text;
            string reason = richTextBox1.Text;
            string desc = richTextBox2.Text;
            string process = richTextBox3.Text;
            int d_num = int.Parse(textBox5.Text);
            int i_num = int.Parse(textBox6.Text);
            double loss = double.Parse(textBox7.Text);
            AccidentInfo accident = new AccidentInfo(date, level, d_num, i_num, loss, reason, desc, process);
            int index = listBox1.SelectedIndex;
            GlobalData.default_pipeline.Accidents[index] = accident;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            GlobalData.default_pipeline.Accidents.RemoveAt(index);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            else
            {
                int index = listBox1.SelectedIndex;
                var accident = GlobalData.default_pipeline.Accidents[index];
                dateTimePicker1.Value = accident.AcDate;
                comboBox1.Text = accident.AcLevel;
                textBox5.Text = accident.DeathNum.ToString();
                textBox6.Text = accident.InjureNum.ToString();
                textBox7.Text = accident.AcLoss.ToString();
                richTextBox1.Text = accident.AcReason;
                richTextBox2.Text = accident.AcDescription;
                richTextBox3.Text = accident.AcProcess;
            }
        }

        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string print_info = "";
            print_info += "管道ID：" + GlobalData.default_pipeline.Id.ToString();
            print_info += "\n管道名称：" + GlobalData.default_pipeline.Name;
            print_info += "\n起始位置：" + GlobalData.default_pipeline.StartLocation;
            print_info += "\n末端位置：" + GlobalData.default_pipeline.EndLocation;
            print_info += "\n事故日期：" + dateTimePicker1.Value.Date.ToString();
            print_info += "\n事故级别：" + comboBox1.Text;
            print_info += "\n死亡人数：" + textBox5.Text;
            print_info += "\n重伤人数：" + textBox6.Text;
            print_info += "\n事故损失：" + textBox7.Text;
            print_info += "\n事故原因：" + richTextBox1.Text;
            print_info += "\n事故描述：" + richTextBox2.Text;
            print_info += "\n事故处理：" + richTextBox3.Text;

            printDialog1.ShowDialog();
            e.Graphics.DrawString(print_info, richTextBox1.Font, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
            printDocument1.DocumentName = GlobalData.default_pipeline.Name + "-管道信息";
        }
    }
}
